﻿using System;
using System.Text;
using System.Collections.Generic;

//自定义命名空间
using DreamCube.Foundation.Basic.Utility;

//第三方DLL
using Microsoft.International.Converters.PinYinConverter;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;

namespace DreamCube.Foundation.International
{
    public static class MyConverter
    {
        /// <summary>
        /// 根据拼音字符串获取所有同音字
        /// </summary>
        /// <param name="pinyins">拼音字符串</param>
        /// <param name="defaultValues">当获取失败时，返回的默认值</param>
        /// <returns></returns>
#if NET20
        public static Char[] GetChars(String pinyins, Char[] defaultValues)
#else
        public static Char[] GetChars(this String pinyins, Char[] defaultValues = null)
#endif
        {
            if (String.IsNullOrEmpty(pinyins)) return defaultValues;
            return ChineseChar.GetChars(pinyins);
        }

        /// <summary>
        /// 获取指定字符串的首字母
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
#if NET20
        public static List<String> GetPYFirstWord(Char target)
#else
        public static List<String> GetPYFirstWord(this Char target)
#endif
        {
            return GetPYFirstWord(new ChineseChar(target));
        }

        /// <summary>
        /// 获取字符串的拼音（不带声调，所以去掉重复的拼音）
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
#if NET20
        public static List<String> GetPYFirstWord(ChineseChar ch)
#else
        public static List<String> GetPYFirstWord(ChineseChar ch)
#endif
        {
            List<String> pys = new List<String>();
            for (Int32 j = 0; j < ch.PinyinCount; j++)
            {
                String pyAndSound = ch.Pinyins[j];
                String py = pyAndSound.Substring(0, 1);
                if (!pys.Contains(py))
                    pys.Add(py);
            }
            return pys;
        }

        /// <summary>
        /// 获取字符串的拼音（不带音标，所以去掉重复的拼音）
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
#if NET20
        public static List<String> GetPYList(Char target)
#else
        public static List<String> GetPYList(this Char target)
#endif
        {
            return GetPYList(new ChineseChar(target));
        }

        /// <summary>
        /// 获取字符串的拼音（不带声调，所以去掉重复的拼音）
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
#if NET20
        public static List<String> GetPYList(ChineseChar ch)
#else
        public static List<String> GetPYList(ChineseChar ch)
#endif
        {
            List<String> pys = new List<String>();
            for (Int32 j = 0; j < ch.PinyinCount; j++)
            {
                String pyAndSound = ch.Pinyins[j];
                String py = pyAndSound.Substring(0, pyAndSound.Length - 1);
                if (!pys.Contains(py))
                    pys.Add(py);
            }
            return pys;
        }

        /// <summary>
        /// 获取字符串的拼音
        /// </summary>
        /// <param name="target"></param>
        /// <param name="separator">字符串中每一个字符的拼音之间的分隔符号（拼音首字母和完整的拼音之间的分隔符）</param>
        /// <param name="includeMutilPY">是否包含多音字的所有组合</param>
        /// <param name="mutilPySeparator">当包含多音字的拼音组合时，每一个组合之间的分隔符(拼音每一个字首字母组合的分隔符)</param>
        /// <param name="includePYFirstWord">标识是否包含拼音的首字母</param>
        /// <returns></returns>
#if NET20
        public static String GetPinyins(String target,
                                        String separator = "",
                                        Boolean includeMutilPY = false,
                                        String mutilPySeparator = ";",
                                        Boolean includePYFirstWord = false)
#else
        public static String GetPinyins(this String target,
                                        String separator = "",
                                        Boolean includeMutilPY = false,
                                        String mutilPySeparator = ";",
                                        Boolean includePYFirstWord = false)
#endif
        {
            if (String.IsNullOrEmpty(target)) return String.Empty;
            if (includeMutilPY) //包含多音字组合的情况
            {
                List<String> pinyinList = new List<String>();
                List<String> firstWordList = new List<String>();
                for (Int32 i = 0; i < target.Length; i++)
                {
                    ChineseChar ch = new ChineseChar(target[i]);
                    List<String> tempPYList = GetPYList(ch);   //拼音全称
                    List<String> tempFirstWordList = GetPYFirstWord(ch);  //拼音首字母
                    if (i == 0)
                    {
                        for (Int32 j = 0; j < tempPYList.Count; j++)  //字体的拼音全称
                            pinyinList.Add(tempPYList[j]);
                        if (includePYFirstWord)
                        {
                            for (Int32 j = 0; j < tempFirstWordList.Count; j++)  //字体的拼音首字母
                                firstWordList.Add(tempFirstWordList[j]);
                        }
                    }
                    else
                    {
                        //拼音全称
                        List<String> newPinyinList = new List<String>();
                        for (Int32 k = 0; k < pinyinList.Count; k++)
                        {
                            for (Int32 j = 0; j < tempPYList.Count; j++)
                                newPinyinList.Add(pinyinList[k] + separator + tempPYList[j]);
                        }
                        if (includePYFirstWord)
                        {
                            //首字母
                            List<String> newPYFirstWord = new List<String>();
                            for (Int32 k = 0; k < firstWordList.Count; k++)
                            {
                                for (Int32 j = 0; j < tempFirstWordList.Count; j++)
                                    newPYFirstWord.Add(firstWordList[k] + separator + tempFirstWordList[j]);
                            }
                            firstWordList = newPYFirstWord;
                        }
                        pinyinList = newPinyinList;
                    }
                }
                if (includePYFirstWord) return MyEnumerable.JoinEx(pinyinList, mutilPySeparator) + mutilPySeparator + MyEnumerable.JoinEx(firstWordList, mutilPySeparator);
                else return MyEnumerable.JoinEx(pinyinList,mutilPySeparator);
            }
            else  //不包含多音字组合的情况
            {
                StringBuilder pinyins = new StringBuilder();
                StringBuilder firstWord = new StringBuilder();
                for (Int32 i = 0; i < target.Length; i++)
                {
                    if (i != 0)
                    {
                        pinyins.Append(separator);
                        firstWord.Append(separator);
                    }
                    ChineseChar ch = new ChineseChar(target[i]);
                    String py = ch.Pinyins[0];
                    if (includePYFirstWord) firstWord.Append(py.Substring(0, 1)); //拼音首字母
                    pinyins.Append(py.Substring(0, py.Length - 1)); //拼音全称
                }
                if (includePYFirstWord) return pinyins.ToString() + separator + firstWord.ToString();
                else return pinyins.ToString();
            }
        }

        /// <summary>
        /// 把繁体字符串转换成简体中文
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
#if NET20
        public static String ToSimplified(String target)
#else
        public static String ToSimplified(this String target)
#endif
        {
            return ChineseConverter.Convert(target, ChineseConversionDirection.TraditionalToSimplified);
        }

        /// <summary>
        /// 把简体字符串转换成繁体字符串
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
#if NET20
        public static String ToTraditional(String target)
#else
        public static String ToTraditional(this String target)
#endif
        {
            return ChineseConverter.Convert(target, ChineseConversionDirection.SimplifiedToTraditional);
        }
    }
}
